package view;

import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.WindowConstants;

import services.DataCache;

import com.toedter.calendar.JCalendar;
//import com.toedter.calendar.JDayChooser;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;
import java.util.Date;

public class DatePickerCellEditor extends AbstractCellEditor
                         implements TableCellEditor
						{
	private static final long serialVersionUID = 5218508953282649827L;

	Date currentDate;
    JButton button;
        
    public DatePickerCellEditor() {
    	button = new JButton();
    	button.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				final MyDatePicker dpFrame = new MyDatePicker();
				dpFrame.setDate(currentDate);
				dpFrame.setSize(new Dimension(600,350));
				//dpFrame.setLocation(, y)
				dpFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				dpFrame.addWindowListener(new WindowListener() {
					@Override
					public void windowClosing(WindowEvent we) {
						
					}

					@Override
					public void windowActivated(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowClosed(WindowEvent e) {
						// TODO Auto-generated method stub
						currentDate = dpFrame.sDate;
						//currentDate = DataCache.DateFormat.format(dSelected);
						button.setText(DataCache.DateFormat.format(currentDate));
					}

					@Override
					public void windowDeactivated(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowDeiconified(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowIconified(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void windowOpened(WindowEvent e) {
						// TODO Auto-generated method stub
						
					}
				});
				dpFrame.setVisible(true);
			}
    		
    	});
        //Set up the dialog that the button brings up.
        
    }

    //Implement the one CellEditor method that AbstractCellEditor doesn't.
    public Object getCellEditorValue() {
        return currentDate;
    }

    //Implement the one method defined by TableCellEditor.
    public Component getTableCellEditorComponent(JTable table,
                                                 Object value,
                                                 boolean isSelected,
                                                 int row,
                                                 int column) {
    	//System.out.println(table.getLocation().x + "," + table.getLocation().y);
    	currentDate = (Date) value;
        return button;
    }
}